//
//  game.c
//  Tic_Tac_Toe
//
//  Created by Daniel on 2024/5/20.
//

#include "game.h"

void Init_board(char board[ROW][COLUMN],int row,int column){
    for (int i=0;i<row;i++){
        for(int j = 0;j<column;j++){
            board[i][j] = ' ';
        }
    }
}
void Display_board(char board[ROW][COLUMN],int row,int column){
//    打印数据
    for (int i = 0;i<row;i++){
//        printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
        for(int j = 0;j<column;j++){
            printf(" %c ",board[i][j]);
            if(j<column-1)
                printf("|");
        }
        printf("\n");
//        打印分割行
//        printf("---|---|---\n");
        if (i<row-1) {
            for (int j = 0; j<column; j++) {
                printf("---");
                if (j<column-1)
                    printf("|");
            }printf("\n");
        }
        
    }
}

void Player_move(char board[ROW][COLUMN],int row,int column){
    int x = 0;
    int y = 0;
    printf("请您下棋:>\n");
    printf("请输入坐标:>");
    while (1) {
        scanf("%d %d",&x,&y);
        //    坐标范围合法的判断
        if(x >= 1 && x <= row && y >=1 && y <= column)
        {
            if(board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*' ;
                break;
            }else
            {
                printf("坐标被占用，请选择其他位置");
            }
        }else
        {
            printf("坐标不在合法范围内，请重新输入\n");
        }
    }
}

void Machine_move(char board[ROW][COLUMN],int row,int column)
{
    printf("电脑下棋:>\n");
    int x = 0;
    int y = 0;
    while (1)
    {
        x = rand() % row;//0~(row-1)
        y = rand() % column;//0~(column-1)
        if(board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }
}

int Be_fulled(char board[ROW][COLUMN],int row,int column)
{
    for (int i = 0; i<row; i++) {
        for (int j = 0; j < column; j++) {
            if (board[i][j] == ' ')
            {
                return 0;
            }
        }
    }
    return 1;
}

char Judgement(char board[ROW][COLUMN],int row,int column)
{
//    我的方法(LOL)
    //    如果玩家赢了
    //    每一行的判断
//    if((board[0][0] == '*' && board[0][1] == '*'&& board[0][2] == '*') || (board[1][0] == '*' && board[1][1] == '*' && board[1][2] == '*') || (board[2][0] == '*' && board[2][1] == '*' && board[2][2] == '*')){
//        printf("恭喜，您赢了\n");
//        return '*';
//    }
//    //    每一列的判断
//    if((board[0][0] == '*' && board[1][0] == '*'&& board[2][0] == '*') || (board[0][1] == '*' && board[1][1] == '*' && board[2][1] == '*') || (board[0][2] == '*' && board[1][2] == '*' && board[2][2] == '*')){
//        printf("恭喜，您赢了\n");
//        return '*';
//    }
//    //    对角线的判断
//    if((board[0][0] == '*' && board[1][1] == '*'&& board[2][2] == '*') || (board[0][2] == '*' && board[1][1] == '*' && board[2][0] == '*')){
//        printf("恭喜，您赢了\n");
//        return '*';
//    }
////    如果电脑赢了
//    //    每一行的判断
//    if((board[0][0] == '#' && board[0][1] == '#'&& board[0][2] == '#') || (board[1][0] == '#' && board[1][1] == '#' && board[1][2] == '#') || (board[2][0] == '#' && board[2][1] == '#' && board[2][2] == '#')){
//        printf("抱歉，您输了\n");
//        return '#';
//    }
//    //    每一列的判断
//    if((board[0][0] == '#' && board[1][0] == '#'&& board[2][0] == '#') || (board[0][1] == '#' && board[1][1] == '#' && board[2][1] == '#') || (board[0][2] == '#' && board[1][2] == '#' && board[2][2] == '#')){
//        printf("抱歉，您输了\n");
//        return '#';
//    }
//    //    对角线的判断
//    if((board[0][0] == '*' && board[1][1] == '*'&& board[2][2] == '*') || (board[0][2] == '*' && board[1][1] == '*' && board[2][0] == '*')){
//        printf("抱歉，您输了\n");
//        return '#';
//    }
//    return 'D';
    
    
//    第二种方法
//    每一行的判断
    for(int i = 0;i<row;i++)
    {
        if(board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] !=' ')
        {
            return board[i][1];
        }
    }
    
//    每一列的判断
    for(int j = 0;j<column;j++)
    {
        if(board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] !=' ')
        {
            return board[1][j];
        }
    }
    
//    对角线的判断
    if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] !=' ')
        {
            return board[1][1];
        }
    
    if(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] !=' ')
    {
        return board[1][1];
    }
    if(Be_fulled(board,row,column))
    {
        return 0;
    }
    return 'D';
}
